package com.oocl.o2o.admin.controller;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oocl.o2o.admin.entity.Advertisement;
import com.oocl.o2o.admin.listener.AdvertisementApplyJMSListener;
import com.oocl.o2o.admin.service.AdvertisementService;
import com.oocl.o2o.admin.util.ImageUtil;
import com.oocl.o2o.admin.vo.AdvertisementVO;

@Controller
@RequestMapping("/sec/advertisement")
public class AdvertisementController {

	private static Logger logger = Logger
			.getLogger(AdvertisementController.class);

	@Resource(name = "advertisementServiceImpl")
	private AdvertisementService advertisementService;

	/**
	 * 
	 * 同步时
	 */

	@GetMapping
	public String showAllAdvertisement(Model model) {

		logger.info("展示所有的广告页面");
		List<Advertisement> advertisementList = advertisementService.findAll();

		model.addAttribute("advertisementList", advertisementList);

		return "sec/showAdvertisementList";
	}

	@GetMapping("/status/{status}")
	public String showAdvertisementWithStatus(
			@PathVariable("status") int status, Model model) {
		logger.info("展示广告页面并且分类");
		List<Advertisement> advertisements = new ArrayList<Advertisement>();
		advertisements = advertisementService.findByApprovalStatus(status);
		if(advertisements==null){
			return "sec/showAdvStatusList";
		}
		model.addAttribute("advertisements", advertisements);
		return "sec/showAdvStatusList";
	}

	/**
	 * 关于广告更改状态， 其业务逻辑： 1.每次更新前，先select 一下是否有6条已经存在数据库当中 2.如果满足了6条，判断是否需要删除
	 * 3.若满足了6条，就该更新是无效的，提醒管理员 4.若不满足6条，就可以继续update，直到满足6条为止
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@ResponseBody
	@Transactional
	@PatchMapping("/cstatus/{status}")
	public String updateAdvertStatus(@PathVariable("status") String status,
			@RequestBody AdvertisementVO advertisementVO) {
		if (Integer.parseInt(status) == 1) {
			List<Advertisement> advertisementList = advertisementService
					.findByApprovalStatus(Integer.parseInt(status));
			logger.info("检查是否小于6的size");
			if (advertisementList.size() < 6) {
				// 可以update
				Advertisement advertisement = advertisementService
						.updateStatusAndSend(advertisementVO);
				if (advertisement != null) {
					return "success";
				} else {
					return "fail";
				}

			} else {
				return "no update";
			}
		} else {
			Advertisement advertisement = advertisementService
					.updateStatusAndSend(advertisementVO);
			return "remove";
		}
	}

	
	@ResponseBody
	@GetMapping("/id/{id}")
	public int getCountByMerchantId(@PathVariable("id") String id, Model model) {

		List<Advertisement> advertisements = advertisementService
				.findByMerchantId(id);
		logger.info("该merchant有条广告" + advertisements.size());
		if (advertisements ==null) {
			return 0;
		} else {
			return advertisements.size();
		}

	}

	@ResponseBody
	@GetMapping("/{id}")
	public AdvertisementVO checkDetail(@PathVariable("id") String id) {
		logger.info("得到该merchant的信息" + id);
		AdvertisementVO advertisementVO = advertisementService
				.findByIdForDetail(id);
		return advertisementVO;

	}

	@GetMapping("/id/{id}/type/{type}")
	public void showImg(@PathVariable("id") String id,
			@PathVariable("type") String type, OutputStream outputStream)
			throws Exception {
		logger.info("写文件");
		String filePath = advertisementService.findByIdForShowImage(id, type);
		ImageUtil.showImage(filePath, outputStream);
	}

	@ResponseBody
	@GetMapping("/count")
	public int getAdvertCount() {
		int count = AdvertisementApplyJMSListener.getAdvertCount();
		AdvertisementApplyJMSListener.setAdvertCount(0);
		return count;

	}

	@ResponseBody
	@GetMapping("/newCount")
	public int getNewAdvertCount() {
		List<Advertisement> list = advertisementService.findByApprovalStatus(0);
		return list.size();
	}

}
